﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using PEC.Data;
using PEC.Util;

namespace PEC
{
    public class PecController : IDisposable
    {
        static PecController _current;
        BUS.BUSControl bus;
        string xuongId;

        public string XuongId
        {
            get { return xuongId; }
            set { xuongId = value; }
        }
        public BUS.BUSControl Bus
        {
            get { return bus; }
            set { bus = value; }
        }
        private FrmMain _mainForm;
        private string[] _commandlineArgs;
        internal bool IsDBConnected = false;

        public string ConnectString { get; set; }
        public int SoChuyen { get; set; }

        internal PecController()
        {
            Init();
            _current = this;
            GetConfig();
        }

        private void Init()
        {
            _mainForm = new FrmMain(this);
            _mainForm.Terminator = new FrmTerminator(this);
            // _mainForm.Terminator.FormClosed += Terminator_FormClosed;
            _mainForm.Show();
        }

        void Terminator_FormClosed(object sender, FormClosedEventArgs e)
        {
            _mainForm.Close();
        }
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            var controller = new PecController();
            controller.MainForm.Closing += controller.MainForm_Closing;
            controller.CommandLineArgs = args;
            // process the command line args here so we have a UI, also, we can not process in Init without
            // modifying more code to take args[]
            controller.ProcessCommandLineArgs();
            Application.Run();

            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            //Terminator = new FrmTerminator();
            //Application.Run(_mainForm);
        }

        private void ProcessCommandLineArgs()
        {
            if (CommandLineArgs.Length > 0)
            {

            }
        }

        void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Application.Exit();
        }

        internal string[] CommandLineArgs
        {
            get { return _commandlineArgs; }
            set { _commandlineArgs = value; }
        }
        public FrmMain MainForm
        {
            get { return _mainForm; }
            set { _mainForm = value; }
        }
        internal static PecController Current
        {
            get { return _current; }
        }
        private void GetConfig()
        {
            GetConfigSystem();
            GetConfigComPorts();
            GetConfigConnectionString();
            GetConfigSoChuyen();
        }

        private void GetConfigSoChuyen()
        {
            var tDir = ConfigTools.Get("SoChuyen");
            if (string.IsNullOrEmpty(tDir))
            {
                tDir = ConfigTools.OriginalGet("SoChuyen");

                while (tDir.LastIndexOf(@"\\") == tDir.Length - 2)
                {
                    tDir = tDir.Substring(0, tDir.Length - 1);
                }
            }

            if (string.IsNullOrEmpty(tDir))
            {
                SoChuyen = 1;
            }
            else
            {
                SoChuyen = Convert.ToInt32(tDir);
            }
        }

        private void GetConfigSystem()
        {
            var tDir = ConfigTools.Get("XuongId");
            if (string.IsNullOrEmpty(tDir))
            {
                tDir = ConfigTools.OriginalGet("XuongId");

                while (tDir.LastIndexOf(@"\\") == tDir.Length - 2)
                {
                    tDir = tDir.Substring(0, tDir.Length - 1);
                }
            }

            if (string.IsNullOrEmpty(tDir))
            {
                XuongId = "Pecas";
            }
            else
            {
                XuongId = tDir;
            }
        }
        internal string ComPorts { get; set; }

        #region internal Methods

        internal void Connect()
        {
            var frmConn = new ConnectionForm();
            var result = frmConn.ShowDialog();
            if (result == DialogResult.OK)
            {
                Cursor.Current = Cursors.WaitCursor;

                Bus = new BUS.BUSControl("S", ConnectionFactory.ConnectionString);
                Cursor.Current = Cursors.Default;
                IsDBConnected = true;
            }
        }

        private void GetConfigComPorts()
        {
            var tDir = ConfigTools.Get("ComPorts");
            if (string.IsNullOrEmpty(tDir))
            {
                tDir = ConfigTools.OriginalGet("ComPorts");

                while (tDir.LastIndexOf(@"\\") == tDir.Length - 2)
                {
                    tDir = tDir.Substring(0, tDir.Length - 1);
                }
            }

            if (string.IsNullOrEmpty(tDir))
            {
                ComPorts = "";
            }
            else
            {
                ComPorts = tDir;
            }
        }

        private void GetConfigConnectionString()
        {
            var tDir = ConfigTools.Get("ConnectString");
            if (string.IsNullOrEmpty(tDir))
            {
                tDir = ConfigTools.OriginalGet("ConnectString");

                while (tDir.LastIndexOf(@"\\") == tDir.Length - 2)
                {
                    tDir = tDir.Substring(0, tDir.Length - 1);
                }
            }

            if (string.IsNullOrEmpty(tDir))
            {
                ConnectString = "Data Source=.;Initial Catalog=CPEC;Persist Security Info=True;";
            }
            else
            {
                ConnectString = tDir;
            }
            Bus = new BUS.BUSControl("S", ConnectString);
        }
        #endregion

        public void Dispose()
        {

        }
    }
}
